package com.cnswhy.cloud.server.merchant.controller.customer;

import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.CnswhyUtil;
import com.cnswhy.cloud.common.logging.uitls.MDCUtil;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.enumeration.TransType;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.common.entity.mq.WithdrawVerifyMQ;
import com.cnswhy.cloud.server.core.entity.*;
import com.cnswhy.cloud.server.core.dto.CustomerWalletSaveDTO;
import com.cnswhy.cloud.server.core.dto.CustomerWalletUpdateDTO;
import com.cnswhy.cloud.server.core.dto.CustomerWalletPageDTO;
import com.cnswhy.cloud.server.core.service.CustomerWalletService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.core.service.WalletTransDtlService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.spell.SpellOrderService;
import com.cnswhy.cloud.server.core.wallet.WalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/customerWallet")
@Api(value = "CustomerWallet", tags = "")
//@PreAuth(replace = "customerWallet:")
public class CustomerWalletController extends SuperController<CustomerWalletService, Long, CustomerWallet, CustomerWalletPageDTO, CustomerWalletSaveDTO, CustomerWalletUpdateDTO> {
    @Autowired
    WalletService walletService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    CustomerService customerService;
    @Autowired
    WalletTransDtlService walletTransDtlService;
    @PostMapping("/repair")
    public R<Boolean> rerepairpairBalance() {
        List<CustomerWallet> customerWalletList = baseService.list();
        for(CustomerWallet customerWallet : customerWalletList){
            if(!customerWallet.getWalletType().eq(WalletType.BALANCE)){
                List<WalletTransDtl> walletTransDtlList = walletTransDtlService.list(Wraps.<WalletTransDtl>lbQ().eq(WalletTransDtl::getCustomerWalletId,customerWallet.getId()).orderByAsc(WalletTransDtl::getId));
                Long curr = null;
                for(WalletTransDtl walletTransDtl : walletTransDtlList){
                    if(curr == null){
                        curr = walletTransDtl.getBeginBal();
                    }else{
                        walletTransDtl.setBeginBal(curr);

                    }
                    walletTransDtl.setEndBal(curr + walletTransDtl.getDedBal());
                    curr = curr + walletTransDtl.getDedBal();
                    walletTransDtlService.updateById(walletTransDtl);
                }
                if(curr != null){
                    customerWallet.setAvlbBalance(curr);
                    baseService.updateById(customerWallet);
                }

            }
        }
        return success();
    }
    @Autowired
    SpellOrderService spellOrderService;
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CustomerWallet> customerWalletList = list.stream().map((map) -> {
            CustomerWallet customerWallet = CustomerWallet.builder().build();
            return customerWallet;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(customerWalletList));
    }
}
